//=============================================================================
#include "Player.h"

#include "SoundBank.h"
#include "Random.h"
#include "log.h"
#include <SFML/Network.hpp>
//=============================================================================
int playerCounter = 1; //this is used for giving players a unique id.

CPlayer::CPlayer() {
	this->health = 100;
	castStart = 0;
	castEnd = 0;
	targetX = 0;
	targetY = 0;
	lastCollision = -10;
	castSpell1 = castSpell2 = castSpell3 = castSpell4 = castSpell5 = 0;
	casting = false;
	facingLeft = true;
	facingRight = false;
	this->Type = ENTITY_TYPE_PLAYER;
	++playerCounter;
	this->playerID = playerCounter;
}

//=============================================================================
bool CPlayer::OnLoad(char* File, int Width, int Height, int MaxFrames) {
    if(CEntity::OnLoad(File, Width, Height, MaxFrames) == false) {
        return false;
    }

    return true;
}

//-----------------------------------------------------------------------------
void CPlayer::OnLoop() {
	if(frozen)
	{
		if(SDL_GetTicks() > freezeTime)
			frozen = false;
		
	}
	if(casting && CAppStateManager::GetActiveAppStateInt() == 5)
	{
		
		if(castEnd <= SDL_GetTicks())
		{
			x.Clear();
			x << 0;
			x << this->spellID;
			x << this->playerID-2;
			x << 11;
			x << targetX;
			x << targetY;
			x << "test";
			CClient::getInstance().ServerBroadcastPacket(x);

			CSoundBank::SoundControl.Play(6, 1); //cast sound-effect
			newProjectile = new CProjectile((X+14) + ((Height)/2)-8, Y+ ((Width)/2)-4, targetX, targetY, this->spellID);
			CEntity::ProjectileList.push_back(newProjectile);
			newProjectile->playerID = this->playerID-2;
			switch(spellID)
			{

				case 0:
				{
					break;
				}
				case 1:
				{
					newProjectile->damage = 0;
					newProjectile->Surf_Entity = Surface_ProjectileSpell;
					newProjectile->Width = 16;
					newProjectile->Height = 16;
					newProjectile->Anim_Control.MaxFrames = 5;
					castSpell5 = SDL_GetTicks() + 6000;
					break;
				}
				case 2:
				{
					newProjectile->damage = 0;
					newProjectile->Surf_Entity = Surface_FreezeSpell;
					newProjectile->Width = 16;
					newProjectile->Height = 16;
					newProjectile->Anim_Control.MaxFrames = 5;
					castSpell3 = SDL_GetTicks() + 10000;
					break;
				}
				case 3:
					{
						castSpell4 = SDL_GetTicks() + 20000;
						flash();
						break;
					}
				case 4:
					{
						newProjectile->Surf_Entity = Surface_FireBallSpell;
						newProjectile->Width = 16;
						newProjectile->Height = 16;
						newProjectile->Anim_Control.MaxFrames = 5;
						newProjectile->damage = 20;
						castSpell1 = SDL_GetTicks() + 500;
						break;
					}
				case 5:
					{
						newProjectile->Surf_Entity = Surface_MagicMissileSpell;
						newProjectile->Width = 16;
						newProjectile->Height = 16;
						newProjectile->Anim_Control.MaxFrames = 5;
						newProjectile->damage = 10;
						castSpell2 = SDL_GetTicks() + 1500;
						break;
					}
				default:
				{
					break;
				}
			}
			casting = false;
		}
	}

	CEntity::OnLoop();
}

void CPlayer::CastProjectile(int identifier)
{
	CSoundBank::SoundControl.Play(6, 1); //cast sound-effect
	newProjectile = new CProjectile((X+14) + ((Height)/2)-8, Y+ ((Width)/2)-4, targetX, targetY, identifier);
	CEntity::ProjectileList.push_back(newProjectile);
	newProjectile->playerID = this->playerID-2;
	switch(identifier)
	{

		case 0:
		{
			break;
		}
		case 1:
		{
			newProjectile->damage = 0;
			newProjectile->Surf_Entity = Surface_ProjectileSpell;
			newProjectile->Width = 16;
			newProjectile->Height = 16;
			newProjectile->Anim_Control.MaxFrames = 5;
			castSpell5 = SDL_GetTicks() + 6000;
			break;
		}
		case 2:
		{
			newProjectile->damage = 0;
			newProjectile->Surf_Entity = Surface_FreezeSpell;
			newProjectile->Width = 16;
			newProjectile->Height = 16;
			newProjectile->Anim_Control.MaxFrames = 5;
			castSpell3 = SDL_GetTicks() + 10000;
			break;
		}
		case 3:
			{
				castSpell4 = SDL_GetTicks() + 20000;
				flash();
				break;
			}
		case 4:
			{
				newProjectile->Surf_Entity = Surface_FireBallSpell;
				newProjectile->Width = 16;
				newProjectile->Height = 16;
				newProjectile->Anim_Control.MaxFrames = 5;
				newProjectile->damage = 20;
				castSpell1 = SDL_GetTicks() + 500;
				break;
			}
		case 5:
			{
				newProjectile->Surf_Entity = Surface_MagicMissileSpell;
				newProjectile->Width = 16;
				newProjectile->Height = 16;
				newProjectile->Anim_Control.MaxFrames = 5;
				newProjectile->damage = 10;
				castSpell2 = SDL_GetTicks() + 1500;
				break;
			}
		default:
		{
			break;
		}
	}
	casting = false;
}

//-----------------------------------------------------------------------------
void CPlayer::OnRender(SDL_Surface* Surf_Display) {
	CEntity::OnRender(Surf_Display,playerID);
}

//------------------------------------------------------------------------------
void CPlayer::OnCleanup() {
	CEntity::OnCleanup();
}

//------------------------------------------------------------------------------
void CPlayer::OnAnimate() {
	if(SpeedX != 0) {
		Anim_Control.MaxFrames = 3;
	}else{
		Anim_Control.MaxFrames = 0;
	}

	CEntity::OnAnimate();
}

//------------------------------------------------------------------------------
bool CPlayer::OnCollision(CEntity* Entity) {
	if(Entity->playerID != this->playerID-2 && Entity->Type == ENTITY_TYPE_PLAYERSPELL && CAppStateManager::GetActiveAppStateInt() == 5 && !Entity->exploded)
	{
		x.Clear();
		x << 0;
		x << Entity->spellIdentifier;
		x << this->playerID-2;
		x << 10;
		x << 0;
		x << 0;
		x << "test";
		CClient::getInstance().ServerBroadcastPacket(x);
		this->RunCollision(Entity);
	}
	
	return true;
}

void CPlayer::RunCollision(CEntity* Entity)
{
	if(Entity->Type == ENTITY_TYPE_PLAYERSPELL && SDL_GetTicks() > (this->oldTime+100))
	{
		
		switch(Entity->spellID)
		{
		case 0:	// Non-effect spells
			{
				break;
			}
		case 1:	// Counterspell
			{
				if(this->casting)
				{
					casting = false;
				}
				CSoundBank::SoundControl.setSoundPosition(CCamera::CameraControl.GetX()+(WWIDTH/2), CCamera::CameraControl.GetY()+(WHEIGHT/2), Entity->X, Entity->Y);
				CSoundBank::SoundControl.Play(7, 1); //fireball sound-effect
				break;
			}
		case 2:	// Freeze
			{
				if(this->casting)
				{
					casting = false;
				}
				frozen = true;
				freezeTime = SDL_GetTicks()+1000;
				break;
			}
		}
		this->health = this->health - Entity->damage;
		Entity->damage = 0;
		this->oldTime = SDL_GetTicks();
		
		if(this->health <= 0)
		{
			CSoundBank::SoundControl.setSoundPosition(CCamera::CameraControl.GetX()+(WWIDTH/2), CCamera::CameraControl.GetY()+(WHEIGHT/2), Entity->X, Entity->Y);
			CSoundBank::SoundControl.Play(Random(4,5), 1); //killed sound-effect
			for(int i = 0; i < CEntity::PlayerList.size(); i++)
			{
				if(CEntity::PlayerList.at(i)->playerID-2 == Entity->playerID)
				{
					CEntity::PlayerList.at(i)->playerScore += 1;
				}
			}
			frozen = true;
			freezeTime = SDL_GetTicks() + 5000;
			if(this->casting)
			{
				casting = false;
			}
			this->X = 300*(this->playerID-1);
			this->Y = 300;
			this->playerDeaths += 1;
			this->health = maxHealth;
			
		}
	}
}


//=============================================================================
